/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.s2aei.controller;

import com.s2aei.model.Administrador;
import com.s2aei.model.Papeis;
import com.s2aei.model.Status;
import com.s2aei.model.Usuario;
import com.s2aei.model.dao.AdministradorDao;
import com.s2aei.model.dao.UsuarioDao;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Christian
 */
public class AdminController {
    private Usuario newUser;
    private Administrador newAdmin;
    
    public void criarAdmin(String email, String nome, String senha){
        UsuarioDao usuarioDao = new UsuarioDao();
        int id = (int) ((int) 1 + (Math.random()*100));
        Date data = new Date();
        newUser = new Usuario(id, email, nome, senha, data);
        newUser.setPapel(new Papeis("Administrador"));
        newUser.setDataCadastro(data);
        newUser.setStatus(new Status("Ativo"));
        newAdmin = new Administrador(id);
        AdministradorDao adminDao = new AdministradorDao();
        try {
            usuarioDao.create(newUser);
            adminDao.create(newAdmin);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
    public List<Usuario> listarAdmin(){
        List<Usuario> userList = new UsuarioDao().findUsuarioEntities();
        List<Usuario> returnList = new ArrayList<Usuario>();
        for (int i = 0; i < userList.size(); i++){
            if("Administrador".equals(userList.get(i).getPapel().getTipo())){
                returnList.add(userList.get(i));
            }
        }
        return returnList;
    }
    
    public void editarAdmin(int id, String email, String nome, String senha){
        AdministradorDao aDao = new AdministradorDao();
        UsuarioDao uDao = new UsuarioDao();
        Administrador adm = aDao.findAdministrador(id);
        adm.getUsuario().setNome(nome);
        adm.getUsuario().setEmail(email);
        adm.getUsuario().setSenha(senha);
        try{
            uDao.edit(adm.getUsuario());
            aDao.edit(adm);
        } catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
    
    public void deletarAdmin(int id){
        AdministradorDao aDao = new AdministradorDao();
        UsuarioDao uDao = new UsuarioDao();
        try{
            aDao.destroy(id);
            uDao.destroy(id);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    
}
